$(document).ready(function(){
    getOrdersList1(ordersListPageNo1,showNum)
})

var status = 10
//切换状态
$('#order_status').on('change',function(){
    status = $(this).children('option:selected').val();
})

$('#searchBtn').on('click',function(){
    p = 1;
    getOrdersList1(p,showNum);
})

/*** 商家订单 ***/
var ordersListPageNo1 = 1,showNum=10;
var ordersList1 = ''
function getOrdersList1(p,showNum){
    let time = '00:00:00'
    let endTime = '23:59:59'
    var start = $('#start').val() != ''? $('#start').val() + ' ' + time: '2019-01-01' + ' ' + time;//开始时间
    var end = $('#end').val() != ''? $('#end').val() + ' ' + endTime: laydate.now(0, 'YYYY-MM-DD') + ' ' + endTime;//结束时间
    start = Date.parse(start) / 1000
    end = Date.parse(end) / 1000
    getAjax(`${global.apiUrl}/get_jf_order`,function(res){//未完
        console.log(res);
        if(res.list.length == 0 || !res.list){
            $("#page").paging({
                pageNo:0,
                totalPage: 0, //总页数
                totalSize: 0,
                callback: function(num) {
                    getOrdersList1(num,showNum)
                }
            });//未完
            var html = `
                    <tr>
                        <td colspan="7" align="center">暂无订单</td>
                    </tr>`
            $('#ordersList1').html(html);
        }else{
            $("#page").paging({
                pageNo:p,
                totalPage: Math.ceil(res.count/showNum), //总页数
                totalSize: res.count,
                callback: function(num) {
                    getOrdersList1(num,showNum)
                    ordersListPageNo1 = num
                }
            });
            var html = '';
            ordersList1 = res.list
            var list = res.list;
            for(var i in list){//未完
                html += `
                    <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].phone}</td>
                        <td>${list[i].order_num}</td>
                        <td>${setTime2(list[i].time)}</td>
                        <td>${list[i].all_jf}</td>
                        <td>${getStatus(list[i].status)}</td>
                        <td class="control-td themeColor">
                            <span data-id="${list[i].id}" data-target="#OrdersModal1" data-toggle="modal" data-type='4'>查看详情</span> 
                            ${getDelivery(list[i].status,list[i].id)}
                        </td>
                    </tr>
            `
            }
            $('#ordersList1').html(html);
        }
    },{
        p:p,
        num:showNum,
        type: 1,
        community_id: global.community_id,
        start_time: start,
        end_time: end,
        status: status
    })
}

//发货
function getDelivery(status,id){
    var span = ''
    if(status == '1'){
        span += `<span class="delivery" data-target="#logisticsModal" data-toggle="modal" data-id="${id}" style="margin-left: 15px">立即发货</span> `
    }
    return span
}

$('#OrdersModal1').on('show.bs.modal',function(event){//未完
    var modal = $(this);
    var btnThis = $(event.relatedTarget);
    var type = btnThis.data('type');
    var ordersId = btnThis.data('id');
    modal.find("#logisticsName2").css('display','none')
    modal.find("#logisticsNum2").css('display','none')
    modal.find(".logistics_name").text('')
    modal.find(".logistics_number").text('')
    //健康商城
    var ordersInfo = '';
    for(var i in ordersList1){
        if(ordersList1[i].id == ordersId){
            ordersInfo = ordersList1[i]
        }
    }
    var html = ''
    html += `<div class="shopItemAre">
                <img src="${ordersInfo.head_img}" class="shopImg"></img>
                <div class="shopItem">
                    <div>${ordersInfo.product_name}</div>
                    <div>${'x'+ ordersInfo.num}</div>
                </div>
            </div>`
    $(".shopMainAre").html(html)
    $('.number').text(ordersInfo.num)
    $('.price').text(ordersInfo.all_jf)
    $('.orderNum').text(ordersInfo.order_num)
    $('.orderDate').text(setTime2(ordersInfo.time))
    $('.orderStatus').text(getStatus(ordersInfo.status))
    $('.name').text(ordersInfo.name)
    $('.phone').text(ordersInfo.phone)
    $('.address').text(ordersInfo.sheng + ordersInfo.shi + ordersInfo.qu + ordersInfo.address)
    if(ordersInfo.address_order_shop){
        $('#logisticsName2').css('display','block')
        $('.logistics_name').text(ordersInfo.address_order_shop)
    }
    if(ordersInfo.address_order_num){
        $('#logisticsNum2').css('display','block')
        $('.logistics_number').text(ordersInfo.address_order_num)
    }
})

$('#logisticsModal').on('show.bs.modal',function(event){//未完
    var modal = $(this);
    var btnThis = $(event.relatedTarget);
    var ordersId = btnThis.data('id');
    modal.find('#logisticsOrderId').val(ordersId);
    var logisticsName = modal.find('#logisticsName');
    logisticsName.val('');
    var logisticsNum = modal.find('#logisticsNum');
    logisticsNum.val('');
})

$('#logisticsBtn').on('click',function(){
    var id = $("#logisticsOrderId").val();
    var logisticsName = $('#logisticsName').val();
    var logisticsNum = $('#logisticsNum').val();
    if(logisticsName == '' || logisticsNum == ''){
        alert('请将物流信息填写完整！')
        return;
    }
    postAjax(`${global.apiUrl}/operation_community_jf_shop_order`,function(res){
        // console.log(res)
        if(res == 1){
          alert('发货成功')
          getOrdersList1(ordersListPageNo1,showNum);
        }
    },{id: id,type: 1,address_order_shop: logisticsName,address_order_num: logisticsNum})
})

function getStatus(status){
    let statusTxt = ''
    switch (status) {
        case '0':
        statusTxt = '待付款'
        break;
        case '1':
        statusTxt = '待发货'
        break;
        case '2':
        statusTxt = '待收货'
        break;
        case '3':
        statusTxt = '已完成'
        break;
        case '4':
        statusTxt = '已评价'
        break;
    }
    return statusTxt
}

